const app = getApp();
const pageOptions = {
  // 页面数据
  data: {
    active: 2,
    isbatch: false,
    result: [],
    list: [],
    is_transference: false,

    isLoadMore: true,
    page: 1,
    total_page: 0
  },
  // 页面载入时
  onLoad(e) {
    this.init(e)
  },
  // 页面初始化
  init(e) {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: []
    })
    this.getData();
  },
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {},
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {
    console.log(this.data.isLoadMore)
    if (!this.data.isLoadMore) {
      return
    } else {
      this.getMoreData();
    }
  },
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {},
  tabChange(e) {
    this.setData({
      active: e.detail.name,
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: []
    })
    this.getData();
  },
  isbatchOpen(e) {
    if(this.data.list.length <= 0) return
    this.setData({
      isbatch: true
    })
  },
  isbatchPost(e) {
    if (this.data.result.length <= 0) {
      this.setData({
        isbatch: false
      })
      return
    }
    this.setData({
      isbatch: false
    })
    this.transferencePost(this.data.result)
  },
  onChange(e) {
    this.setData({
      result: e.detail
    })
  },
  transferenceOnces(e) {
    let {
      ids
    } = e.currentTarget.dataset;
    let cid = [ids]
    this.transferencePost(cid);
  },
  async cancelSell(e){
    let {id} = e.currentTarget.dataset;
    if(!id){
      return
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child_id.cancelSell"),
      data: {
        child_id: id
      },
    })
    if(data.result == 1){
      app.tips(data.msg);
      this.getData();
    }else{
      app.tips(data.msg);
    }
  },
  async transferencePost(ids) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child-id.transference"),
      data: {
        child_ids: ids
      },
    })
    if (data.result) {
      app.tips(data.msg)
      if (this.data.isbatch) {
        this.setData({
          isbatch: !this.data.isbatch
        })
      }
      this.getData();
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let urlstr = "plugin.project-party.frontend.controllers.child-id.index";
    let json = {
      page: this.data.page
    }
    if (this.data.active == 3) {
      urlstr = "plugin.project-party.frontend.controllers.child-id.sellDetails";
    } else {
      json.status = this.data.active
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlstr),
      data: json,
    })
    let res = data;
    if (!res.result) {
      app.tips(res.msg);
      return
    }
    if(this.data.active == 2){
      this.setData({
        is_transference:res.data.is_transference?true:false
      })
    }
    this.setData({
      list: res.data.list?.data || [],
      total_page: res.data.list?.pages,
      isLoadMore: true
    })
    console.log(this.data.active,data.data.is_transference)
    if (this.data.active == 2) {
      this.setData({
        is_transference : data.data.is_transference ? true : false
      })
    }
  },
  async getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    }
    this.setData({
      page: this.data.page + 1
    })
    let urlstr = "plugin.project-party.frontend.controllers.child-id.index";
    let json = {
      page: this.data.page
    }
    if (this.data.active == 3) {
      urlstr = "plugin.project-party.frontend.controllers.child-id.sellDetails";
    } else {
      json.status = this.data.active
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlstr),
      data: json,
    })
    let res = data;
    this.data.isLoadMore = true;
    if (!res.result) {
      app.tips(res.msg)
      this.data.isLoadMore = false;
      return
    }
    let nextData = res.data.list.data;
    this.setData({
      list: this.data.list.concat(nextData)
    })
  }
}

Page(pageOptions)